<template>
   <div>
       <el-button type="primary" @click="returnsupplier">返回</el-button>
        <el-form ref="form" :model="editsuppliergoods" label-width="80px">
              <el-form-item label="商品名字">
                  <el-input v-model="editsuppliergoods.nickname"></el-input>
              </el-form-item>
               <el-form-item label="商品类别">
                  <el-input v-model="editsuppliergoods.category"></el-input>
              </el-form-item>
               <el-form-item label="商品材质">
                  <el-input v-model="editsuppliergoods.material"></el-input>
              </el-form-item>
               <el-form-item label="适用类型">
                  <el-input v-model="editsuppliergoods.specifications"></el-input>
              </el-form-item>
              <el-form-item label="专用">
                  <el-input v-model="editsuppliergoods.exclusive"></el-input>
              </el-form-item>
                <el-form-item label="包装规格">
                  <el-input v-model="editsuppliergoods.packaging"></el-input>
              </el-form-item>
              <el-form-item label="商品口味">
                  <el-input v-model="editsuppliergoods.taste"></el-input>
              </el-form-item>
               <el-form-item label="特殊功用">
                  <el-input v-model="editsuppliergoods.special"></el-input>
              </el-form-item>
               <el-form-item label="产地">
                  <el-input v-model="editsuppliergoods.origin"></el-input>
              </el-form-item>
               <el-form-item label="生产日期">
                  <el-input v-model="editsuppliergoods.manufacturedate"></el-input>
              </el-form-item>
              <el-form-item label="保质期">
                  <el-input v-model="editsuppliergoods.shelflife"></el-input>
              </el-form-item>
              <el-form-item label="特色说明">
                  <el-input v-model="editsuppliergoods.featuresthat"></el-input>
              </el-form-item>
              <el-form-item label="价格">
                  <el-input v-model="editsuppliergoods.price"></el-input>
              </el-form-item>
              <el-form-item label="商品图片">
                   <el-upload
                        class="avatar-uploader"
                        action="/api/supplier/upload/add"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="editsuppliergoods.img" :src="editsuppliergoods.img" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                     
              </el-form-item>
               <el-button type="primary" @click="uploadsuppliergoods">修改</el-button>
            </el-form>
   </div>
</template>


<script>
import suppliergoodsApi from "../../services/suppliergoods";
export default {
  data() {
    return {
      dialogVisible: false
    };
  },
  async created() {
    const data = await suppliergoodsApi.getSuppliergoodsById(
      this.$route.query.id
    );
    this.$store.commit("suppliergoods/setEditSuppliergoods", data.data);
  },
  methods: {
    handleAvatarSuccess(res) {
      this.$store.state.suppliergoods.editSuppliergoods.img = res.data[0]; //服务器的图片路径
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    returnsupplier() {
      //返回
      this.$router.push({
        name: "查看货品"
      });
    },
    uploadsuppliergoods() {
      //修改
      const data = this.$store.state.suppliergoods.editSuppliergoods;
      this.$store.dispatch("suppliergoods/modifySuppliergoods", data);
      this.$message("修改成功");
      setTimeout(() => {
        this.$router.push({
          name: "查看货品"
        });
      }, 1000);
    }
  },
  computed: {
    editsuppliergoods() {
      return this.$store.state.suppliergoods.editSuppliergoods;
    }
  }
};
</script>


<style scoped>
.el-input {
  width: 400px;
}


.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
